import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/list.dart';

enum CustomGradientProgressWidget {
  vals,
  count,
  percent,
  total,
  ;
}

typedef CustomGradientProgressIndicators = ({List<CustomGradientProgressWidget> center, List<CustomGradientProgressWidget> right});

class CustomGradientProgress extends StatelessWidget {
  CustomGradientProgress({
    required this.title,
    required this.vals,
    super.key,
    this.colors = const [
      Color.fromARGB(255, 178, 237, 245),
      Colors.blue,
    ],
    this.titleLeftStyle,
    this.titleCenterStyle,
    this.titleRightStyle,
    this.direction = Axis.horizontal,
    this.reverse = false,
    this.indicators = _defaultIndicators,
    this.showDotOnProgress = true,
  });

  final String title;
  final ({int actual, int total}) vals;
  final List<Color> colors;
  final TextStyle? titleLeftStyle;
  final TextStyle? titleCenterStyle;
  final TextStyle? titleRightStyle;
  final Axis direction;
  final bool reverse;
  final CustomGradientProgressIndicators indicators;
  final bool showDotOnProgress;

  static const _defaultIndicators = (
    center: [
      CustomGradientProgressWidget.percent,
    ],
    right: [
      CustomGradientProgressWidget.vals,
    ]
  );
  static const CustomGradientProgressIndicators onlyRightCount = (
    center: [],
    right: [
      CustomGradientProgressWidget.count,
    ]
  );
  static const CustomGradientProgressIndicators centerTotalRightPercent = (
    center: [
      CustomGradientProgressWidget.total,
    ],
    right: [
      CustomGradientProgressWidget.percent,
    ]
  );

  late final percent = () {
    final val = vals.actual / vals.total;
    if (val.isNaN) return 0;
    return val;
  }();

  late final _right = titleRightStyle ??
      TextStyle(
        fontSize: 12.w,
        color: colors.lastOrNull,
        fontWeight: FontWeight.w500,
      );
  late final _center = titleCenterStyle ??
      TextStyle(
        fontSize: 12.w,
        color: const Color(0xFF495061),
      );

  late final bool _isHorizontal = direction == Axis.horizontal;
  late final (Alignment begin, Alignment end) _gradientDirection = (
    _isHorizontal ? (reverse ? Alignment.centerRight : Alignment.centerLeft) : (reverse ? Alignment.bottomCenter : Alignment.topCenter),
    _isHorizontal ? (reverse ? Alignment.centerLeft : Alignment.centerRight) : (reverse ? Alignment.topCenter : Alignment.bottomCenter),
  );

  @override
  Widget build(BuildContext context) {
    final percent_ = percent == 0 ? 0.05 : percent;
    final aligns = _gradientDirection;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Text(
              title,
              style: titleLeftStyle ?? TextStyle(fontSize: 12.w, color: const Color(0x80333333)),
            ),
            AppGaps.hGap6,
            if (indicators.center.isNotEmpty) SvgPicture.asset(AppImages.icon_red_triangle),
            AppGaps.hGap4,
            // ignore: unnecessary_cast
            ...(indicators.center
                    .map(
                      (e) => _buildIndicator(e, true),
                    )
                    .toList() as List<Widget>)
                .joinWith(AppGaps.hGap4),
            const Spacer(),
            // ignore: unnecessary_cast
            ...(indicators.right
                    .map(
                      (e) => _buildIndicator(e, false),
                    )
                    .toList() as List<Widget>)
                .joinWith(AppGaps.hGap4),
          ],
        ),
        AppGaps.vGap4,
        Stack(
          children: [
            LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
              return Container(
                height: 6.w,
                margin: EdgeInsets.only(top: 5.w, bottom: 6.w, right: 4.w),
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: aligns.$1,
                    end: aligns.$2,
                    colors: colors,
                  ),
                  borderRadius: BorderRadius.circular(17.w),
                ),
                width: percent_ * constraints.maxWidth,
              );
            }),
            if (showDotOnProgress)
              Positioned(
                right: 0,
                child: Image.asset(
                  '${AppImages.image}icon_progress_comp.png',
                  width: 16.w,
                  height: 16.w,
                ),
              )
          ],
        )
      ],
    );
  }

  Widget _buildIndicator(CustomGradientProgressWidget type, bool center) {
    late final String text;
    switch (type) {
      case CustomGradientProgressWidget.vals:
        text = '${vals.actual}/${vals.total}';
        break;
      case CustomGradientProgressWidget.count:
        text = '${vals.actual}';
        break;
      case CustomGradientProgressWidget.percent:
        text = '${(percent * 100).ceil()}%';
        break;
      case CustomGradientProgressWidget.total:
        text = '总数: ${vals.total}';
        break;
    }
    return Text(
      text,
      style: center ? _center : _right,
    );
  }
}
